package com.example.finalhomework.thirdhomework

import android.graphics.Bitmap
import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.finalhomework.R

@Composable
fun FourthScreen_3(navController: NavHostController = rememberNavController()) {
    // 使用 remember 保存拍摄的图片
    var imageBitmap by remember { mutableStateOf<Bitmap?>(null) }

    // 启动相机应用拍照，并返回拍摄的图片
    //rememberLauncherForActivityResult 用于创建并记住一个 ActivityResultLauncher，可以用来启动一个活动并获取结果
    val takePictureLauncher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicturePreview()) { bitmap ->
        if (bitmap != null) {
            imageBitmap = bitmap // 保存拍摄的图片
            Log.d("FourthScreen_3", "Photo taken successfully")
        } else {
            Log.e("FourthActivity", "No camera app found to handle the intent") // 记录错误日志
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize() // 填充整个屏幕
            .padding(16.dp), // 设置内边距为16dp
        verticalArrangement = Arrangement.Center, // 垂直居中排列
        horizontalAlignment = Alignment.CenterHorizontally // 水平居中排列
    ) {

        Image(
            painter = painterResource(id = R.drawable.background6),
            contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp)
        )

        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 按钮，点击后启动相机拍照
            Button(onClick = {
                takePictureLauncher.launch(null)
                Log.d("FourthScreen_3", "Take Photo button clicked")
            }) {
                Text("Take Photo") // 按钮上的文本
            }
            Spacer(modifier = Modifier.height(16.dp)) // 间隔16dp的空白
            // 显示拍摄的图片
            if (imageBitmap != null) {
                Image(
                    //Bitmap 是 Android 的原生图像类型，而 ImageBitmap 是 Jetpack Compose 中用于图像显示的类型
                    bitmap = imageBitmap!!.asImageBitmap(), //将 Bitmap 转换为 ImageBitmap
                    contentDescription = null,
                    modifier = Modifier.size(200.dp)
                )
                Log.d("FourthScreen_3", "Displaying taken photo")
            } else {    //如果 imageBitmap 为空，则显示提示文本
                Text("You haven't taken any photos yet.")
                Log.d("FourthScreen_3", "No photo to display")
            }
        }

        Box(
            modifier = Modifier
                .fillMaxSize(),
            contentAlignment = Alignment.BottomEnd
        ) {
            Image(
                painter = painterResource(id = R.drawable.background7),
                contentDescription = null,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp)
            )
        }
    }
    Log.d("FourthScreen_3", "Composable function executed")
}

@Preview(showBackground = true)
@Composable
fun FourthScreenPreview() {
    FourthScreen_3()
}